package com.qp;

import cn.hutool.core.date.DateUtil;
import com.qp.opc.OpcUaClientFacade;
import com.qp.opc.OpcUaClientFactory;
import com.qp.opc.OpcUaConfig;
import com.qp.opc.OpcUaUtil;
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OpcRead {
    //日志
    private static final Logger log = LoggerFactory.getLogger(OpcUaClientFactory.class);

    public static void main(String[] args) {
        OpcUaConfig opcConfig = new OpcUaConfig().setUrl("opc.tcp://192.168.31.110:49320");
        OpcUaClientFacade client;
        try {
            client = OpcUaClientFactory.getClient(opcConfig);
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }
//        try {
//            Set<String> names = OpcUaUtil.browse(client, "Channel_SWL.DeviceSWL.OP100");
//            List<String> keyList = new ArrayList<>();
//            keyList.addAll(names);
//            Collections.sort(keyList);
//            for (String name : keyList) {
//                System.out.println(name);
//            }
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }

        while (true) {
            try {
                List<String> opcKeyList = new ArrayList<>();
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.WORKCELL_CODE");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.LINE_CODE");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.START_TIME");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.END_TIME");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.TORQUE_OK_1");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.TORQUE_OK_2");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.TORQUE_OK_3");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.TORQUE_OK_4");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.TYPE_CODE");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.MT_BARCODE_L");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.MT_BARCODE_R");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.MT_CODE_L");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.MT_CODE_R");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.PARTS_BN_L_15");
                opcKeyList.add("Channel_SWL.DeviceSWL.OP100.PARTS_BN_R_15");
                Map<String, Object> opcValueMap = OpcUaUtil.readValuesToMap(client, opcKeyList);
                log.info("");
                log.info("");
                log.info("**************************************************************************");
                if (opcValueMap != null) {
                    for (Map.Entry<String, Object> entry : opcValueMap.entrySet()) {
                        if (entry.getValue() instanceof DateTime) {
                            Date temp = ((DateTime) entry.getValue()).getJavaDate();
                            temp = DateUtil.offsetHour(temp,-8);
                            log.info("Key: {}，值：{}", entry.getKey(), DateUtil.formatDateTime(temp));
                        } else {
                            log.info("Key: {}，值：{}", entry.getKey(), entry.getValue());
                        }

                    }
                }
                log.info("**************************************************************************");
                log.info("");
                Thread.sleep(60000);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}